package com;


import entity.Admin;
import entity.Role;
import mapper.AdminMapper;
import mapper.RoleMapper;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import service.api.AdminService;
import service.api.RoleService;
import service.impl.AdminServiceImpl;

import javax.sound.midi.Soundbank;
import javax.sql.DataSource;
import java.sql.Connection;

// 指定 Spring 给 Junit 提供的运行器类
@RunWith(SpringJUnit4ClassRunner.class)
// 加载 Spring 配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml","classpath:spring-persist-tx.xml"})
public class Test {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleMapper RoleMapper;

    @Autowired
    private RoleService RoleService;

    @org.junit.Test
    public void testAddData() {
        for (int i= 0; i< 238; i++){
            adminMapper.insert(new Admin(null, "loginAcct" + i, "userPswd" + i, "userName" + i, "email" + i, null));
        }
    }

    @org.junit.Test
    public void testAddRole() {
        for (int i= 0; i< 235; i++){
            RoleMapper.insert(new Role(null,"Role"+i));
        }
    }

    @org.junit.Test
    public void  testTx() {
        Admin admin = new Admin(null, "jerry", "123", "杰瑞", "jerry@qq.com", null);
        adminService.saveAdmin(admin);


    }

    @org.junit.Test
    public void testLog() {
        //1 获取log 对象
        Logger logger = LoggerFactory.getLogger(Test.class);

        //2根据不同的日志级别打印日志
        logger.debug("Hello i am Debug");
        logger.debug("Hello i am Debug");
        logger.debug("Hello i am Debug");


        logger.info("i am info");
        logger.info("i am info");
        logger.info("i am info");

        logger.error("i am errro");
        logger.error("i am errro");
        logger.error("i am errro");

    }

    @org.junit.Test
    public void insertAdminMapper() {
        Admin admin = new Admin(null, "tom", "123123", "汤姆", "tom@qq.com", null);
        int insert = adminMapper.insert(admin);
        System.out.println("搜影响的行数"+insert);

        //在实际开发中，所有想查看sout打印方式，会给项目带来问题
        //sout本质是io流，通常io流对项目消耗是很大的
        //可以通过日志系统，通过日志j级别可以批量打印控制信息
    }

    @org.junit.Test
    public void testConnection()  throws  Exception{
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }
}
